package circuits;

import java.util.HashMap;
import java.util.Iterator;

public class SimpleComponent extends Component{

	private Operation operation;
	private HashMap<String,Operand> operands;
	
	public SimpleComponent(){
		this.operation = null;
		this.operands = new HashMap<String,Operand>();
	}
	
	public boolean setOperand(String inputName, Operand operand){
		if (!this.operands.containsKey(inputName)){
			this.operands.put(inputName, operand);
			return true;
		}
		return false;
	}
	
	public void setOperation(Operation operation){
		this.operation = operation;
	}

	private void loadOperands(){
		Iterator<String> inputNamesIterator = this.inputValues.keySet().iterator();
		while(inputNamesIterator.hasNext()){
			String inputName = inputNamesIterator.next();
			Boolean booleanValue = this.inputValues.get(inputName);
			Operand operand = this.operands.get(inputName);
			operand.setResult(booleanValue.booleanValue());
		}
	}
	
	@Override
	public void readInputValues(){
		this.inputValues.clear();
		Iterator<String> inputNamesIterator = this.connectedInputs.keySet().iterator();
		while(inputNamesIterator.hasNext()){
			String inputName = inputNamesIterator.next();
			Input input = this.connectedInputs.get(inputName);
			Boolean booleanValue = new Boolean(input.getCurrentState());
			this.inputValues.put(inputName,booleanValue);
		}
	}
	
	@Override
	public void generateOutput() {
		this.loadOperands();
		this.output.setCurrentState(this.operation.getResult());
	}
	
	public boolean readOutput(){
		return this.output.getCurrentState();
	}
}
